<template>
  <div class="bgi">
    <div class="login">
    <el-form
    ref="login"
    :model="user"
    :rules="rules"
    label-width="120px"
    class="demo-ruleForm"
  >
    <el-form-item label="用户名" prop="userName">
      <el-input v-model="user.userName" autocomplete="off" />
    </el-form-item>
    <el-form-item label="密码" prop="pwd">
      <el-input v-model="user.pwd" type="password" autocomplete="off" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="userLogin">提交</el-button
      >
      <el-button @click="resetForm">重置</el-button>
    </el-form-item>
  </el-form>
  </div>
  </div>
</template>

<script>
import { userLogin } from '@/api/login/login.js'

export default {
  name: 'LoginPage',
  data () {
    return {
      user: {
        userName: '',
        pwd: ''
      },
      rules: {
        userName: [
          { required: true, message: '用户名不能为空', trigger: 'blur' }
        ],
        pwd: [
          { required: true, message: '密码不能为空', trigger: 'blur' }
        ]
      }
    }
  },

  methods: {
    userLogin () {
      this.$refs.login.validate().then(() => {
        // 提交
        userLogin(this.user).then(res => {
          if (res.data.code === 200) {
            // 立即缓存， 为了持久化
            localStorage.setItem('role', res.data.data.role)
            localStorage.setItem('token', res.data.data.token)
            localStorage.setItem('userInfo', JSON.stringify(res.data.data.userInfo))
            // 存储到vuex中
            this.$store.commit('user/INIT_USER_INFO', res.data.data)
            this.$message({
              type: 'success',
              message: '登录成功',
              duration: 1000,
              onClose: () => {
                this.$router.replace('/DashBoard')
              }
            })
          }
        })
      })
    },

    resetForm () {
      this.$refs.login.resetFields()
    }
  }
}
</script>

<style lang="scss" scoped>
.bgi{
  background-image: url(../../assets/imgs/bgi.jpg);
  height: 100vh;
  background-size: cover;
  background-repeat: no-repeat;
  // overflow: hidden;
  position: relative;
  display: flex;
}
.login{
  margin: auto;
  border-radius: 5%;
  // margin: 100px auto;
  background-color: rgba(0, 0, 0, 0.3);
}
.el-form{
  padding: 50px 50px 50px;
}
</style>
